package org.example.menu.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import org.example.menu.domain.SysMenu;
import org.example.menu.service.SysMenuService;
import org.example.menu.service.SysRoleMenuService;
import org.example.menu.util.JwtUtils;
import org.example.menu.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("menu")
public class MenuController{
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @RequestMapping("list")
    public Result list(HttpServletRequest request){
        String token = request.getHeader("token");
        Map map = (Map) JwtUtils.parseToken(token);
        Integer uid = (Integer) map.get("id");
        System.out.println(uid);
        List<SysMenu> nodeList=sysRoleMenuService.getList(uid+"");
        System.out.println(nodeList);
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getPersonId()+"");
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
                    tree.putExtra("path", treeNode.getPath());
                    tree.putExtra("component", treeNode.getComponent());
                });
        System.out.println("主线程");
        return Result.success(treeNodes);
    }
    @RequestMapping("index")
    public CompletableFuture<Result> index(){
        CompletableFuture<Result> resultCompletableFuture = CompletableFuture.supplyAsync(() -> {
            List<SysMenu> nodeList = sysMenuService.list();
            //配置
            TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
            //转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
            List<Tree<String>> treeNodes = TreeUtil.<SysMenu, String>build(nodeList, "0", treeNodeConfig,
                    (treeNode, tree) -> {
                        tree.setId(treeNode.getId() + "");
                        tree.setParentId(treeNode.getPersonId() + "");
                        tree.setName(treeNode.getName());
                        // 扩展属性 ...
                        tree.putExtra("path", treeNode.getPath());
                        tree.putExtra("component", treeNode.getComponent());
                    });
            return Result.success(treeNodes);
        },threadPoolTaskExecutor);
        return resultCompletableFuture;

    }
    @RequestMapping("getMenu")
    public Result getMenuList(){
        List<SysMenu> list = sysMenuService.list();
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        //转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<SysMenu, String>build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getPersonId()+"");
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
                    tree.putExtra("path", treeNode.getPath());
                    tree.putExtra("component", treeNode.getComponent());
                });
        return Result.success(treeNodes);
    }
}
